|
|
|
| |
|
|
|
|
PublisherComments was used as the name for the table used in the preceding example. When creating a Microsoft Access database, we could easily have named the table ''Publisher Comments" (note the space), in which case, we would have had to enclose the table name in brackets in the preceding query, like so: |
|
|
|
|
|
|
|
|
This is a good time to point out that it is a bad idea to use embedded spaces as table names. Not only do you need to include brackets around the name in a query, but also the Wizard available to up-size Access data files to Microsoft SQL Server does not work successfully on tables with spaces embedded in their names. |
|
|
|
|
|
|
|
|
|
|
The preceding SQL statement returns all the records from the Publisher table that have a PubID that matches a PubID in the [Publisher Comments] table. This type of JOIN returns all the records that reside within both tablesthus, an INNER JOIN. |
|
|
|
|
|
|
|
|
This is handy if you have two tables that you know are not perfectly matched against a single column and you want to create a result set that contains only those rows that match on both sides. The INNER JOIN also works well when you have a parent table (such as a CustomerTable) and a child table (such as a ShipAddressTable) with a one-to-one relationship. Using an INNER JOIN, you can quickly create a list of all CustomerTable records that have a corresponding ShipAddressTable record on file. |
|
|
|
|
|
|
|
|
INNER JOINs work best when you create a JOIN on a column that is unique in both tables. If you use a table that has more than one occurrence of the JOIN column, you'll get a row for each occurrence in the result set. This might be undesirable. The following example illustrates the point (see Figure 8.29): |
|
|
|
|
|
|
|
|
SELECT Titles.Title,BookSales.Units
FROM Titles INNER JOIN BookSales
ON Titles.Title = BookSales.Title |
|
|
|
|
|
|
|
|
In the previous example, the table BookSales has numerous entries for each title (one for each quarter recorded), so the result of the INNER JOIN returns each Title multiple times. |
|
|
|
|
|
|
|
|
The LEFT JOIN is one of the two outer joins in the SQL syntax. Although INNER JOIN returns only those rows that have corresponding values in both tables, the outer joins return all the records from one side of the join, whether or not there is a corresponding match on the other side of the join. The LEFT JOIN clause returns all the records from the first table on the list (the left-most table) and any records on the right side of the table that have a matching column value. Figure 8.30 shows the same SQL query that was shown in Figure 8.28. |
|
|
|
|
|